<?php
if(!session_id()) { session_start(); }
include_once('db.class.php');
include_once('stock.class.php');
class Portfolio {
	function add($call_put, $stock_name, $stock_price, $option_price, $expiry, $interest_rate, $volatility, $result) {
		$db = new db();
		$stock = new Stock();
		$stock->AddPopularity($stock_name);
		$user_id = $_SESSION['user_id'];
		$time = time();
		$real_name_of_stock = $stock->GetName($stock_name);
		$stock_name = $real_name_of_stock[0]['stock_name'];
		$results = $db->insert("INSERT INTO portfolios(user_id, portfolio_type, portfolio_name, portfolio_price, portfolio_option_price, portfolio_time_expire, portfolio_interest_rate, portfolio_volatility, portfolio_result, portfolio_unix_time) VALUES('$user_id', '$call_put', '$stock_name', '$stock_price', '$option_price', '$expiry', '$interest_rate', '$volatility', '$result', '$time')");
		if($results) {
			return 800;
		}
		return 1;
	}
	
	function add_black($call_put, $stock_name, $stock_price, $option_price, $expiry, $interest_rate, $volatility, $result) {
		$db = new db();
		$user_id = $_SESSION['user_id'];
		$time = time();
		$stock = new Stock();
		$stock->AddPopularity($stock_name);
		$real_name_of_stock = $stock->GetName($stock_name);
		$stock_name = $real_name_of_stock[0]['stock_name'];
		$results = $db->insert("INSERT INTO portfolios_black(user_id, portfolio_type, portfolio_name, portfolio_price, portfolio_option_price, portfolio_time_expire, portfolio_interest_rate, portfolio_volatility, portfolio_result, portfolio_unix_time) VALUES('$user_id', '$call_put', '$stock_name', '$stock_price', '$option_price', '$expiry', '$interest_rate', '$volatility', '$result', '$time')");
		if($results) {
			return 800;
		}
		return 1;
	}
	
	function retrieveAll() {
		$db = new db();
		$user_id = $_SESSION['user_id'];
		$results = $db->select("SELECT * FROM portfolios WHERE user_id = $user_id");
		if($results != 0) {
			return $results;
		}
		return 0;
	}
	
	function retrieveAllBlack() {
		$db = new db();
		$user_id = $_SESSION['user_id'];
		$results = $db->select("SELECT * FROM portfolios_black WHERE user_id = $user_id");
		if($results != 0) {
			return $results;
		}
		return 0;
	}
	
	function deletePortfolio($id) {
		$db = new db();
		$user_id = $_SESSION['user_id'];
		$results = $db->delete("DELETE FROM portfolios WHERE portfolio_id = '$id'");
		if($results == 800) {
			return 800;
		}
		return 0;
	}
	
	function deletePortfolioBlack($id) {
		$db = new db();
		$user_id = $_SESSION['user_id'];
		$results = $db->delete("DELETE FROM portfolios_black WHERE portfolio_id = '$id'");
		if($results == 800) {
			return 800;
		}
		return 0;
	}
	
	function getCalculatorType() {
		$db = new db();
		$results = $db->select("SELECT calculator_type FROM calculators");
		return $results[0]['calculator_type'];
	}
	
	function changeCalculatorType($newType) {
		$db = new db();
		$results = $db->update("UPDATE calculators SET calculator_type = '$newType'");
		return 1;
	}
}
?>